﻿using Login.ADL;
using Login.Model;

namespace Login.BLL
{
    public class tb_SweekBLL
    {

        //通过班级ID找到对应班级
        #region
        public static UseModel getClassName(string classId)
        {
            return tb_Sweek.getClassName(classId);
        }
        #endregion


        //不上课周数组提取
        #region
        public static string[] getWeeks(string id)
        {
            return tb_Sweek.getWeeks(id);
        }
        #endregion

        //存入指定班级不上课周
        #region
        public static void getcun(string classId, int n)
        {
            tb_Sweek.getcun(classId, n);
        }
        public static void UpdateEmpty(string classId)
        {
            tb_Sweek.UpdateEmpty(classId);
        }
        #endregion 

        //开始排课
        #region
        public static int postClassShu(string sql)
        {
          return  Helpsql.getshu(sql);
        }
        public static int getShiYanShu(string banjiId)
        {
            tb_shiyanclass tys = new tb_shiyanclass();
            return tys.getExpermentShu(tb_shangkeClass.getClassName(banjiId));
        }
        public static int[] getExpermentNumZu(string banjiId)
        {
            return tb_Sweek.getExpermentNumZu(banjiId);
        }
        public static int PaiKeFou(string BanJiMing, int k)
        {
            return tb_Sweek.PaiKeFou(BanJiMing,k);
        }
        public static void shangKeZhouCunQu(string zhou, int n, string id, int a)
        {
            tb_Sweek.shangKeZhouCunQu(zhou, n, id, a);
        }
        #endregion

        /// <summary>
        /// 下面的封装方法是排列班级实验数为奇数的排课方法
        /// </summary>
        /// <returns></returns>
        #region
        public static bool TiaoZheng(int BanJiId,int k,int TimeId)
        {
            string Week = "Week" + k;
            string Zhou = "";
            int a = 0;
            string ShiYanOne = tb_Sweek.ShiYanMingTiqu(BanJiId, Week, 1);
            for (int i = 10; i < 18; i++)
            {
                Zhou = "Week" + i;
                a= tb_Sweek.ZhouShiYanFou(Zhou,BanJiId);
                if (a==1)
                {
                    int n= YanZheng(i,BanJiId,Week,Zhou,TimeId,k,a);
                    if (n == 1)
                    {
                        break;
                    }
                }
                if (i==17)
                {
                    tb_Sweek.ShiYanHuiFu1(BanJiId, k, ShiYanOne);
                    return false;  
                }
            }
            return true;
        }

        public static int YanZheng(int b, int BanJiId, string Week, string Zhou, int TimeId,int k,int a)
        {
            string ShiYanOne = tb_Sweek.ShiYanMingTiqu(BanJiId, Week, 1);
            string ShiYanTwo = tb_Sweek.ShiYanMingTiqu(BanJiId, Zhou, 1);
            string ShiYanThree = tb_Sweek.ShiYanMingTiqu(BanJiId, Zhou, 2);
            tb_Sweek.ShiYanDelete(BanJiId, k, b);
            string[] week = new string[2];
            XuanZeZhouOne(week, Zhou, a, BanJiId, TimeId, ShiYanOne, ShiYanTwo, ShiYanThree);
            if (week[0] == null || week[1] == null)
            {
                week = new string[2];
                int pan= XuanZeZhou(week, Zhou, a, BanJiId, TimeId, ShiYanOne, ShiYanThree, ShiYanTwo);
                if (week[0] == null || week[1] == null)
                {
                    if (pan != 1)
                    {
                        tb_Sweek.ShiYanHuiFu(BanJiId, b, ShiYanTwo, ShiYanThree);
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                }
                else
                {
                    tb_Sweek.ShiYanCunRu(BanJiId, ShiYanOne, 1, week[1]);
                    tb_Sweek.ShiYanCunRu(BanJiId, ShiYanThree, 2, week[1]);
                    tb_Sweek.ShiYanCunRu(BanJiId, ShiYanTwo, 1, week[0]);
                    tb_Sweek.ShiYanCunRu(BanJiId, ShiYanOne, 2, week[0]);
                    return 1;
                }
            }
            else
            {
                tb_Sweek.ShiYanCunRu(BanJiId, ShiYanOne, 1, week[0]);
                tb_Sweek.ShiYanCunRu(BanJiId, ShiYanThree, 2, week[0]);
                tb_Sweek.ShiYanCunRu(BanJiId, ShiYanTwo, 1, week[1]);
                tb_Sweek.ShiYanCunRu(BanJiId, ShiYanOne, 2, week[1]);
                return 1;
            }
        }

        public static void XuanZeZhouOne(string[] week, string Zhou, int a, int BanJiId, int TimeId, string ShiYanOne, string ShiYanTwo, string ShiYanThree)
        {
            for (int c = 2; c < 18; c++)
            {
                Zhou = "Week" + c;
                a = tb_Sweek.ZhouShiYanFou(Zhou, BanJiId);
                if (a == 0)
                {
                    int m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, ShiYanOne);
                    if (m == 0)
                    {
                        if (week[0] == null)
                        {
                            m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, ShiYanThree);
                            if (m == 0)
                            {
                                week[0] = Zhou;
                            }
                        }
                        else
                        {
                            m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, ShiYanTwo);
                            if (m == 0)
                            {
                                week[1] = Zhou;
                            }
                        }
                    }
                }
                if (week[0] != null && week[1] != null)
                {
                    break;
                }
            }
        }

        public static int XuanZeZhou(string[] week, string Zhou, int a, int BanJiId, int TimeId, string ShiYanOne, string ShiYanTwo, string ShiYanThree)
        {
            for (int c = 2; c < 18; c++)
            {
                Zhou = "Week" + c;
                a = tb_Sweek.ZhouShiYanFou(Zhou, BanJiId);
                if (a == 0)
                {
                    int m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, ShiYanOne);
                    if (m == 0)
                    {
                        if (week[0] == null)
                        {
                            m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, ShiYanThree);
                            if (m == 0)
                            {
                                week[0] = Zhou;
                            }
                        }
                        else
                        {
                            m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, ShiYanTwo);
                            if (m == 0 )
                            {
                                week[1] = Zhou;
                            }
                        }
                    }
                }
                if (week[0] !=null && week[1] !=null)
                {
                    break; 
                }
            }
            if (week[0] == null || week[1] == null) 
            {
               int m= QiuYingClick(BanJiId,ShiYanOne ,ShiYanTwo,TimeId);
               int n= QiuYingClick(BanJiId, ShiYanThree, ShiYanOne, TimeId);
               if (m==1 && n==1)
               {
                   return 1; 
               }
            }
            return 0;
        }

        public static int QiuYingClick(int BanJiId, string ShiYanOne, string ShiYanThree, int TimeId)
        {
            for (int c = 2; c < 18; c++)
            {
                int n = 0;
                string Zhou = "Week" + c;
                int a = tb_Sweek.ZhouShiYanFou(Zhou, BanJiId);
                if (a != 0)
                {
                    int b = tb_Sweek.NzhouShiYan(Zhou, ShiYanOne, ShiYanThree, TimeId, BanJiId);
                    if (b == 0)
                    {
                        string ShiYanFour = tb_Sweek.ShiYanMingTiqu(BanJiId, Zhou, 1);
                        string ShiYanFive = tb_Sweek.ShiYanMingTiqu(BanJiId, Zhou, 2);
                        tb_Sweek.ShiYanCunRu(BanJiId, ShiYanOne, 1, Zhou);
                        tb_Sweek.ShiYanCunRu(BanJiId, ShiYanThree, 2, Zhou);
                        for (int i = 2; i < 18; i++)
                        {
                            Zhou = "Week" + i;
                            a = tb_Sweek.ZhouShiYanFou(Zhou, BanJiId);
                            if (a == 0)
                            {
                                int m = tb_Sweek.NzhouShiYan(Zhou, ShiYanFour, ShiYanFive, TimeId, BanJiId);
                                if (m==0)
                                {
                                    tb_Sweek.ShiYanCunRu(BanJiId, ShiYanFour, 1, Zhou);
                                    tb_Sweek.ShiYanCunRu(BanJiId, ShiYanFive, 2, Zhou);
                                    n++;
                                }
                            }
                            if (i==17 && n==0)
                            {
                                Zhou = "Week" + c;
                                tb_Sweek.ShiYanCunRu(BanJiId, ShiYanFour, 1, Zhou);
                                tb_Sweek.ShiYanCunRu(BanJiId, ShiYanFive, 2, Zhou);
                            }
                        }
                    }
                }
                if (n !=0)
                {
                    return 1;
                }
            }
            return 0;
        }
        #endregion

        /// <summary>
        /// 将第十七周尽量缩减
        /// </summary>
        /// <param name="TimeId"></param>
        #region
        public static void YaSuo(int TimeId)   
        {
            //搜索在这个时间点第十七周实验个数
            int n=tb_Sweek.WeekSYshu(17,TimeId);
            if (n%2==0)
            {
                //得到时间点上第十七周的所有实验
                string[] arr = tb_Sweek.ShiYanMingQu(17, TimeId, n);
                for (int i = 0; i < n; i = i + 2)
                {
                    // 通过周实验找到相关班级
                    int BanJiId = tb_Sweek.CalssId(arr[i], TimeId);
                    for (int c = 2; c < 17; c++)
                    {
                        string Zhou = "Week" + c;
                        // 判断班级对应的周是否有实验课
                        int a = tb_Sweek.ZhouShiYanFou(Zhou, BanJiId);
                        if (a == 0)
                        {
                            // 判断在同一时间同一周中是否含有相关的实验名
                            int m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, arr[i]);
                            if (m == 0)
                            {
                                // 判断在同一时间同一周中是否含有相关的实验名
                                m = tb_Sweek.ShiYanCunFou(TimeId, Zhou, arr[i + 1]);
                                if (m == 0)
                                {
                                    // 同一时间同一周中不含有相关的实验名时，存入当前周的列
                                    tb_Sweek.ShiYanChuLi(Zhou,arr[i],arr[i+1],BanJiId);
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 将各周列里的最少项置换到第十七周
        /// </summary>
        /// <param name="TimeId">时间id</param>
        public static void ZhiHuan(int TimeId)
        {
            // 时间点对应周相应的实验数
            int a = tb_Sweek.WeekSYshu(17, TimeId);
            int ZhouShu = 17;
            for (int i = 2; i < 17; i++)
            {
                int n=tb_Sweek.WeekSYshu(i,TimeId);
                if (n<a)
                {
                    a = n;
                    ZhouShu = i;
                }
            }
            if (ZhouShu!=17)
            {
                // 两列周上的实验数交换
                tb_Sweek.SHUJUJIOAHUAN(ZhouShu,TimeId); 
            }
        }

        #endregion


        //班级ID提取
        #region
        public static string getClassId(string banJi)
        {
            return tb_shangkeClass.getClassId(banJi);
        }
        #endregion

        //判断第十七周是否还有实验
        #region
        public static int panDuanShiYan(string id)
        {
            return tb_Sweek.panDuanShiYan(id);
        }
        #endregion

        //排课离散
        #region
        public static void FourLie()
        {
            tb_Sweek.FourLie();
        }
        #endregion
    }
}
